package org.example.reflection;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * @version: 1
 * @description: java
 * @author: java
 * @create: 2020-06-04 18:06
 **/
public class classLoaderTest2 {
    public static void main(String[] args) throws Exception {


    }

    @Test
    public void test1() throws Exception {
        //获取一个系统类加载器
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
        System.out.println(systemClassLoader);
        //获取系统类加载器的父类加载器，即扩展类加载器
        ClassLoader parentClassLoader1 = systemClassLoader.getParent();
        System.out.println(parentClassLoader1);
        //获取引导类加载器
        ClassLoader parent2 = parentClassLoader1.getParent();
        System.out.println(parent2);
        //测试当前类是由哪个类加载器加载
        ClassLoader classLoader = Class.forName("org.example.reflection.classLoaderTest2").getClassLoader();
        System.out.println(classLoader);
        //测试Object类是由哪个类加载器加载
        ClassLoader objClassLoader = Class.forName("java.lang.Object").getClassLoader();
        System.out.println(objClassLoader);
        //关于类加载器一个重要方法getResourceAsStream(String str) 获取类路径下指定文件的输入流
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("app.properties");
        System.out.println(in);
    }

    @Test
    public void test2() throws Exception {
        Class clazz = Class.forName("org.example.reflection.Admin");
        /*Class[] interfaces = clazz.getInterfaces();
        for (Class anInterface : interfaces) {
            System.err.println(anInterface.toString());
        }
        Class superclass = clazz.getSuperclass();
        System.out.println(superclass);
        System.out.println("===================");
        Constructor[] constructors = clazz.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
        System.out.println("===================");
        Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
            System.out.println("构造器名称：" + declaredConstructor.getName());
            System.out.println("构造器修饰符：" + declaredConstructor.getModifiers());
            for (Class parameterType : declaredConstructor.getParameterTypes()) {
                System.out.println("构造器参数类型：" + parameterType);
            }
        }*/

        /*Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println("全部：" + method);
            System.out.println("返回值类型：" + method.getReturnType());
            for (Class parameterType : method.getParameterTypes()) {
                System.out.println("返回值参数：" + parameterType);
            }
            System.out.println("修饰符：" + method.getModifiers());
            for (Class exceptionType : method.getExceptionTypes()) {
                System.out.println(exceptionType);
            }

        }*/
        Annotation annot = clazz.getAnnotation(AnnoDemo.class);
        System.out.println(annot);
        System.out.println("===============================");
        /*for (Method method : clazz.getMethods()) {
            System.out.println(method);
        }*/

        
    }
}

